home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / ztrans.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  11.1 KB  |  427 lines

  1. /* Copyright (C) 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: ztrans.c,v 1.12 2000/09/19 19:00:55 lpd Exp $ */
  20. /* Transparency operators */
  21. #include "string_.h"
  22. #include "ghost.h"
  23. #include "oper.h"
  24. #include "gscspace.h"        /* for gscolor2.h */
  25. #include "gscolor2.h"
  26. #include "gsipar3x.h"
  27. #include "gstrans.h"
  28. #include "gxiparam.h"        /* for image enumerator */
  29. #include "idict.h"
  30. #include "idparam.h"
  31. #include "ifunc.h"
  32. #include "igstate.h"
  33. #include "iimage.h"
  34. #include "iimage2.h"
  35. #include "iname.h"
  36. #include "store.h"
  37.  
  38. /* ------ Utilities ------ */
  39.  
  40. private int
  41. set_float_value(i_ctx_t *i_ctx_p, int (*set_value)(P2(gs_state *, floatp)))
  42. {
  43.     os_ptr op = osp;
  44.     double value;
  45.     int code;
  46.  
  47.     if (real_param(op, &value) < 0)
  48.     return_op_typecheck(op);
  49.     if ((code = set_value(igs, value)) < 0)
  50.     return code;
  51.     pop(1);
  52.     return 0;
  53. }
  54.  
  55. private int
  56. current_float_value(i_ctx_t *i_ctx_p,
  57.             float (*current_value)(P1(const gs_state *)))
  58. {
  59.     os_ptr op = osp;
  60.  
  61.     push(1);
  62.     make_real(op, current_value(igs));
  63.     return 0;
  64. }
  65.  
  66. private int
  67. enum_param(const ref *pnref, const char *const names[])
  68. {
  69.     const char *const *p;
  70.     ref nsref;
  71.  
  72.     name_string_ref(pnref, &nsref);
  73.     for (p = names; *p; ++p)
  74.     if (r_size(&nsref) == strlen(*p) &&
  75.         !memcmp(*p, nsref.value.const_bytes, r_size(&nsref))
  76.         )
  77.         return p - names;
  78.     return_error(e_rangecheck);
  79. }
  80.  
  81. /* ------ Graphics state operators ------ */
  82.  
  83. private const char *const blend_mode_names[] = {
  84.     GS_BLEND_MODE_NAMES, 0
  85. };
  86.  
  87. /* <modename> .setblendmode - */
  88. private int
  89. zsetblendmode(i_ctx_t *i_ctx_p)
  90. {
  91.     os_ptr op = osp;
  92.     int code;
  93.  
  94.     check_type(*op, t_name);
  95.     if ((code = enum_param(op, blend_mode_names)) < 0 ||
  96.     (code = gs_setblendmode(igs, code)) < 0
  97.     )
  98.     return code;
  99.     pop(1);
  100.     return 0;
  101. }
  102.  
  103. /* - .currentblendmode <modename> */
  104. private int
  105. zcurrentblendmode(i_ctx_t *i_ctx_p)
  106. {
  107.     os_ptr op = osp;
  108.     const char *mode_name = blend_mode_names[gs_currentblendmode(igs)];
  109.     ref nref;
  110.     int code = name_enter_string(mode_name, &nref);
  111.  
  112.     if (code < 0)
  113.     return code;
  114.     push(1);
  115.     *op = nref;
  116.     return 0;
  117. }
  118.  
  119. /* <0..1> .setopacityalpha - */
  120. private int
  121. zsetopacityalpha(i_ctx_t *i_ctx_p)
  122. {
  123.     return set_float_value(i_ctx_p, gs_setopacityalpha);
  124. }
  125.  
  126. /* - .currentopacityalpha <0..1> */
  127. private int
  128. zcurrentopacityalpha(i_ctx_t *i_ctx_p)
  129. {
  130.     return current_float_value(i_ctx_p, gs_currentopacityalpha);
  131. }
  132.  
  133. /* <0..1> .setshapealpha - */
  134. private int
  135. zsetshapealpha(i_ctx_t *i_ctx_p)
  136. {
  137.     return set_float_value(i_ctx_p, gs_setshapealpha);
  138. }
  139.  
  140. /* - .currentshapealpha <0..1> */
  141. private int
  142. zcurrentshapealpha(i_ctx_t *i_ctx_p)
  143. {
  144.     return current_float_value(i_ctx_p, gs_currentshapealpha);
  145. }
  146.  
  147. /* <bool> .settextknockout - */
  148. private int
  149. zsettextknockout(i_ctx_t *i_ctx_p)
  150. {
  151.     os_ptr op = osp;
  152.  
  153.     check_type(*op, t_boolean);
  154.     gs_settextknockout(igs, op->value.boolval);
  155.     pop(1);
  156.     return 0;
  157. }
  158.  
  159. /* - .currenttextknockout <bool> */
  160. private int
  161. zcurrenttextknockout(i_ctx_t *i_ctx_p)
  162. {
  163.     os_ptr op = osp;
  164.  
  165.     push(1);
  166.     make_bool(op, gs_currenttextknockout(igs));
  167.     return 0;
  168. }
  169.  
  170. /* ------ Rendering stack operators ------ */
  171.  
  172. private int
  173. rect_param(gs_rect *prect, os_ptr op)
  174. {
  175.     double coords[4];
  176.     int code = num_params(op, 4, coords);
  177.  
  178.     if (code < 0)
  179.     return code;
  180.     prect->p.x = coords[0], prect->p.y = coords[1];
  181.     prect->q.x = coords[2], prect->q.y = coords[3];
  182.     return 0;
  183. }
  184.  
  185. private int
  186. mask_op(i_ctx_t *i_ctx_p,
  187.     int (*mask_proc)(P2(gs_state *, gs_transparency_channel_selector_t)))
  188. {
  189.     int csel;
  190.     int code = int_param(osp, 1, &csel);
  191.  
  192.     if (code < 0)
  193.     return code;
  194.     code = mask_proc(igs, csel);
  195.     if (code >= 0)
  196.     pop(1);
  197.     return code;
  198.  
  199. }
  200.  
  201. /* <paramdict> <llx> <lly> <urx> <ury> .begintransparencygroup - */
  202. private int
  203. zbegintransparencygroup(i_ctx_t *i_ctx_p)
  204. {
  205.     os_ptr op = osp;
  206.     os_ptr dop = op - 4;
  207.     gs_transparency_group_params_t params;
  208.     gs_rect bbox;
  209.     int code;
  210.  
  211.     check_type(*dop, t_dictionary);
  212.     check_dict_read(*dop);
  213.     gs_trans_group_params_init(¶ms);
  214.     if ((code = dict_bool_param(dop, "Isolated", false, ¶ms.Isolated)) < 0 ||
  215.     (code = dict_bool_param(dop, "Knockout", false, ¶ms.Knockout)) < 0
  216.     )
  217.     return code;
  218.     code = rect_param(&bbox, op);
  219.     if (code < 0)
  220.     return code;
  221.     params.ColorSpace = gs_currentcolorspace(igs);
  222.     code = gs_begin_transparency_group(igs, ¶ms, &bbox);
  223.     if (code < 0)
  224.     return code;
  225.     pop(5);
  226.     return code;
  227. }
  228.  
  229. /* - .discardtransparencygroup - */
  230. private int
  231. zdiscardtransparencygroup(i_ctx_t *i_ctx_p)
  232. {
  233.     if (gs_current_transparency_type(igs) != TRANSPARENCY_STATE_Group)
  234.     return_error(e_rangecheck);
  235.     return gs_discard_transparency_layer(igs);
  236. }
  237.  
  238. /* - .endtransparencygroup - */
  239. private int
  240. zendtransparencygroup(i_ctx_t *i_ctx_p)
  241. {
  242.     return gs_end_transparency_group(igs);
  243. }
  244.  
  245. /* <paramdict> <llx> <lly> <urx> <ury> .begintransparencymask - */
  246. private int tf_using_function(P3(floatp, float *, void *));
  247. private int
  248. zbegintransparencymask(i_ctx_t *i_ctx_p)
  249. {
  250.     os_ptr op = osp;
  251.     os_ptr dop = op - 4;
  252.     gs_transparency_mask_params_t params;
  253.     ref *pparam;
  254.     gs_rect bbox;
  255.     int num_components =
  256.     gs_color_space_num_components(gs_currentcolorspace(igs));
  257.     int code;
  258.     static const char *const subtype_names[] = {
  259.     GS_TRANSPARENCY_MASK_SUBTYPE_NAMES, 0
  260.     };
  261.  
  262.     check_type(*dop, t_dictionary);
  263.     check_dict_read(*dop);
  264.     if (dict_find_string(dop, "Subtype", &pparam) <= 0)
  265.     return_error(e_rangecheck);
  266.     if ((code = enum_param(pparam, subtype_names)) < 0)
  267.     return code;
  268.     gs_trans_mask_params_init(¶ms, code);
  269.     if ((code = dict_floats_param(dop, "Background", num_components,
  270.                   params.Background, NULL)) < 0
  271.     )
  272.     return code;
  273.     else if (code > 0)
  274.     params.has_Background = true;
  275.     if (dict_find_string(dop, "TransferFunction", &pparam) >0) {
  276.     gs_function_t *pfn = ref_function(pparam);
  277.  
  278.     if (pfn == 0 || pfn->params.m != 1 || pfn->params.n != 1)
  279.         return_error(e_rangecheck);
  280.     params.TransferFunction = tf_using_function;
  281.     params.TransferFunction_data = pfn;
  282.     }
  283.     code = rect_param(&bbox, op);
  284.     if (code < 0)
  285.     return code;
  286.     code = gs_begin_transparency_mask(igs, ¶ms, &bbox);
  287.     if (code < 0)
  288.     return code;
  289.     pop(5);
  290.     return code;
  291. }
  292. /* Implement the TransferFunction using a Function. */
  293. private int
  294. tf_using_function(floatp in_val, float *out, void *proc_data)
  295. {
  296.     float in = in_val;
  297.     gs_function_t *const pfn = proc_data;
  298.  
  299.     return gs_function_evaluate(pfn, &in, out);
  300. }
  301.  
  302. /* - .discardtransparencymask - */
  303. private int
  304. zdiscardtransparencymask(i_ctx_t *i_ctx_p)
  305. {
  306.     if (gs_current_transparency_type(igs) != TRANSPARENCY_STATE_Mask)
  307.     return_error(e_rangecheck);
  308.     return gs_discard_transparency_layer(igs);
  309. }
  310.  
  311. /* <mask#> .endtransparencymask - */
  312. private int
  313. zendtransparencymask(i_ctx_t *i_ctx_p)
  314. {
  315.     return mask_op(i_ctx_p, gs_end_transparency_mask);
  316. }
  317.  
  318. /* <mask#> .inittransparencymask - */
  319. private int
  320. zinittransparencymask(i_ctx_t *i_ctx_p)
  321. {
  322.     return mask_op(i_ctx_p, gs_init_transparency_mask);
  323. }
  324.  
  325. /* ------ Soft-mask images ------ */
  326.  
  327. /* <dict> .image3x - */
  328. private int mask_dict_param(P5(os_ptr, image_params *, const char *, int,
  329.                    gs_image3x_mask_t *));
  330. private int
  331. zimage3x(i_ctx_t *i_ctx_p)
  332. {
  333.     os_ptr op = osp;
  334.     gs_image3x_t image;
  335.     ref *pDataDict;
  336.     image_params ip_data;
  337.     int num_components =
  338.     gs_color_space_num_components(gs_currentcolorspace(igs));
  339.     int ignored;
  340.     int code;
  341.  
  342.     check_type(*op, t_dictionary);
  343.     check_dict_read(*op);
  344.     gs_image3x_t_init(&image, NULL);
  345.     if (dict_find_string(op, "DataDict", &pDataDict) <= 0)
  346.     return_error(e_rangecheck);
  347.     if ((code = pixel_image_params(i_ctx_p, pDataDict,
  348.                    (gs_pixel_image_t *)&image, &ip_data,
  349.                    12)) < 0 ||
  350.     (code = dict_int_param(pDataDict, "ImageType", 1, 1, 0, &ignored)) < 0
  351.     )
  352.     return code;
  353.     /*
  354.      * We have to process the masks in the reverse order, because they
  355.      * insert their DataSource before the one(s) for the DataDict.
  356.      */
  357.     if ((code = mask_dict_param(op, &ip_data, "ShapeMaskDict", num_components,
  358.                 &image.Shape)) < 0 ||
  359.     (code = mask_dict_param(op, &ip_data, "OpacityMaskDict", num_components,
  360.                 &image.Opacity)) < 0
  361.     )
  362.     return code;
  363.     return zimage_setup(i_ctx_p, (gs_pixel_image_t *)&image,
  364.             &ip_data.DataSource[0],
  365.             image.CombineWithColor, 1);
  366. }    
  367.  
  368. /* Get one soft-mask dictionary parameter. */
  369. private int
  370. mask_dict_param(os_ptr op, image_params *pip_data, const char *dict_name,
  371.         int num_components, gs_image3x_mask_t *pixm)
  372. {
  373.     ref *pMaskDict;
  374.     image_params ip_mask;
  375.     int ignored;
  376.     int code, mcode;
  377.  
  378.     if (dict_find_string(op, dict_name, &pMaskDict) <= 0)
  379.     return 1;
  380.     if ((mcode = code = data_image_params(pMaskDict, &pixm->MaskDict, &ip_mask, false, 1, 12)) < 0 ||
  381.     (code = dict_int_param(pMaskDict, "ImageType", 1, 1, 0, &ignored)) < 0 ||
  382.     (code = dict_int_param(pMaskDict, "InterleaveType", 1, 3, -1,
  383.                    &pixm->InterleaveType)) < 0 ||
  384.     (code = dict_floats_param(op, "Matte", num_components, pixm->Matte, NULL)) < 0
  385.     )
  386.     return code;
  387.     pixm->has_Matte = code > 0;
  388.     /*
  389.      * The MaskDict must have a DataSource iff InterleaveType == 3.
  390.      */
  391.     if ((pip_data->MultipleDataSources && pixm->InterleaveType != 3) ||
  392.     ip_mask.MultipleDataSources ||
  393.     mcode != (pixm->InterleaveType != 3)
  394.     )
  395.     return_error(e_rangecheck);
  396.     if (pixm->InterleaveType == 3) {
  397.     /* Insert the mask DataSource before the data DataSources. */
  398.     memmove(&pip_data->DataSource[1], &pip_data->DataSource[0],
  399.         (countof(pip_data->DataSource) - 1) *
  400.         sizeof(pip_data->DataSource[0]));
  401.     pip_data->DataSource[0] = ip_mask.DataSource[0];
  402.     }
  403.     return 0;
  404. }
  405.  
  406. /* ------ Initialization procedure ------ */
  407.  
  408. const op_def ztrans_op_defs[] = {
  409.     {"1.setblendmode", zsetblendmode},
  410.     {"0.currentblendmode", zcurrentblendmode},
  411.     {"1.setopacityalpha", zsetopacityalpha},
  412.     {"0.currentopacityalpha", zcurrentopacityalpha},
  413.     {"1.setshapealpha", zsetshapealpha},
  414.     {"0.currentshapealpha", zcurrentshapealpha},
  415.     {"1.settextknockout", zsettextknockout},
  416.     {"0.currenttextknockout", zcurrenttextknockout},
  417.     {"5.begintransparencygroup", zbegintransparencygroup},
  418.     {"0.discardtransparencygroup", zdiscardtransparencygroup},
  419.     {"0.endtransparencygroup", zendtransparencygroup},
  420.     {"5.begintransparencymask", zbegintransparencymask},
  421.     {"0.discardtransparencymask", zdiscardtransparencymask},
  422.     {"1.endtransparencymask", zendtransparencymask},
  423.     {"1.inittransparencymask", zinittransparencymask},
  424.     {"1.image3x", zimage3x},
  425.     op_def_end(0)
  426. };
  427.